/**
 * 
 */
package com.gkwave.module.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.gkwave.module.bean.User;

/**
 * @author Dhanasekar Rengasamy
 * @version 1.0
 * 
 */
public class UserDAO extends BaseDAO {

	private Statement stmt = null;
	private ResultSet rset = null;
	private Connection con = null;

	/**
	 * 
	 */
	public UserDAO() {
		// TODO Auto-generated constructor stub
	}

	public User findUser(String query) {
		System.out.println("Query: " + query);
		User user = null;
		BaseDAO dao = new BaseDAO();
		try {
			con = dao.openConnection();
			stmt = con.createStatement();
			rset = stmt.executeQuery(query);

			if (rset.next()) {
				String nmFname = rset.getString("NM_FULLNAME");
				Integer idUser = rset.getInt("id_user");
				user = new User(idUser,nmFname);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dao.closeConnection(stmt, con, rset);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}

	public boolean createUser(String username, String password,
			String fullname, String idEmail) {
		// TODO Auto-generated method stub
		String query = "INSERT INTO m_user  (id_login,nm_password,nm_fullname,id_email,in_active,id_roll) VALUES " +
					   "('"+ username + "','" + password + "','" + fullname + "','"+ idEmail + "','Y',2)";
		System.out.println(query);
		boolean returnValue = false;
		BaseDAO dao = new BaseDAO();
		try {
			con = dao.openConnection();
			stmt = con.createStatement();
			int row = stmt.executeUpdate(query);
			if (row != 0) {
				returnValue = true;
			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			returnValue = false;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			returnValue = false;
		} finally {
			try {
				dao.closeConnection(stmt, con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return returnValue;
	}

	public List<User> getUsers() {
		// TODO Auto-generated method stub
		String query = "SELECT * FROM m_user";
		System.out.println("Query: " + query);
		User user = null;
		BaseDAO dao = new BaseDAO();
		List<User> list = null;
		try {
			con = dao.openConnection();
			stmt = con.createStatement();
			rset = stmt.executeQuery(query);
			if(!rset.wasNull()){
				list = new ArrayList<User>();
			}
			while (rset.next()) {
				String nmFname = rset.getString("nm_fullname");
				String idLogin = rset.getString("id_login");
				String nmPassword = rset.getString("nm_password");
				String idEmail = rset.getString("id_email");
				String inActive = rset.getString("in_active");
				user = new User(nmFname,idLogin,nmPassword,idEmail,inActive);
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dao.closeConnection(stmt, con, rset);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
}
